package com.mrx.pvzcommunity.controller;

import com.mrx.pvzcommunity.model.Result;
import com.mrx.pvzcommunity.model.pvz.Zombie;
import com.mrx.pvzcommunity.service.ZombieService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "僵尸操作")
@RequestMapping(value = "/zombie")
@CrossOrigin(originPatterns = "*", allowCredentials = "true")

public class ZombieController {

    private final ZombieService zombieService;

    @GetMapping("/zombies")
    @ApiOperation(value = "查询僵尸", notes = "查询所有僵尸 API")

    public Result<List<Zombie>> getPlants() {
        return zombieService.getAllZombies();
    }

    @RequestMapping(value = "/img/{id}", method = RequestMethod.GET, produces = {MediaType.IMAGE_PNG_VALUE})
    @ApiOperation(value = "获取僵尸图片", notes = "获取僵尸图片 API", produces = MediaType.IMAGE_PNG_VALUE)
    @ApiImplicitParam(name = "id", value = "僵尸 id", dataTypeClass = Integer.class, paramType = "path", required = true, example = "1")

    public ResponseEntity<byte[]> getPlantImgById(@PathVariable String id) {
        byte[] res = zombieService.getImgByZombieId(id);
        if (res == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(res);
    }

}
